/**
 * 1.连接数据库
 * 2.创建并启动服务
 */


let {
    count
} = require('./utils/contants');
(async (count) => {
    const connect = require('./db/connect')
    const express = require('express')
    const verifyToken = require('./middlewares/verifyToken')
    // 连接数据库
    await connect()

    // 创建并启动服务
    const app = express()

    // 配置中间件
    // 配置静态资源
    app.use(express.static('public'))
    // 配置跨域
    app.use(require('./middlewares/setCORS'))
    // 配置解析参数的
    app.use(express.urlencoded({
        extended: false
    }))
    app.use(express.json())
    app.use((req, res, next) => {
        count++
        next()
    })
    // 配置路由器
    app.use('/admin', require('./routers/loginRouter'))
    app.use('/admin', verifyToken, require('./routers/userRouter'))
    app.use('/count', (req, res) => {
        res.send({
            count
        })
    })


    app.listen(5000, () => {
        console.log('服务启动成功：http://localhost:5000');
    })
})(count)